package com.wzy.listen;

import javax.activation.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class DatasourceListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext servletContext = sce.getServletContext();
        try {
           Context context = new InitialContext();
            javax.sql.DataSource source = (javax.sql.DataSource) context.lookup("java:comp/env/jdbc/news");
            servletContext.setAttribute("source",source);

        }catch (NamingException e){
            e.printStackTrace();
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("web容器销毁");
    }
}
